//
//  MSBuidingIncorporationController.m
//  觅尚
//
//  Created by No.8 on 2019/5/3.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSBuidingIncorporationController.h"
#import "MSBuidingCorporationCell.h"
#import "MSBuidingModel.h"
#import "MSCodeBuidingController.h"
#import "MSScanBuidingController.h"

@interface MSBuidingIncorporationController ()<UITableViewDataSource, UITableViewDelegate,MSBuidingCorporationCellDelegate>
{
    CustomNavigationBar *naviagtionBar;
    
}
@property (nonatomic, strong) UITableView *tableView;
/** 数组*/
@property(nonatomic,strong)NSMutableArray *buidingArray;


/** 商户id*/
@property(nonatomic,strong)NSString *ste_id;


@end

@implementation MSBuidingIncorporationController

/** Nav实现 */
- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    self.buidingArray = [NSMutableArray new];
    
    Prepare *header = [Prepare headerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiHeaderRefresh)];
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    // 马上进入刷新状态
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
    
}
- (void)MJChiBaoZiHeaderRefresh{
    [self ReloadData];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createNavigationBar];
    
    [self tableView];
    
    [self CreatButton];
    
}
- (void)ReloadData{
    [self.buidingArray removeAllObjects];
    NSDictionary * params = @{@"uid":[WYCAccountTool unarchiveuid]};
    [HTTPRequest POST:MSqueryCompany params:params succeed:^(id responseObject) {
        WYCLog(@"------%@",responseObject);
        [self.tableView.mj_header endRefreshing];
        if (CodeStr) {
            self.buidingArray = [MSBuidingModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
        }else{
            [MBManager showBriefAlert:responseObject[@"msg"]];
            
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        WYCLog(@"------%@",error);
    }];
    
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, [self navBarBottom], ScreenWidth, ScreenHeight - [self navBarBottom] - 400 * px) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [[UIView alloc]init];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}
- (void)CreatButton{
    UIView * BackView = [[UIView alloc] initWithFrame:CGRectMake(30 * px, ScreenHeight - 300 * px, ScreenWidth - 60 * px, 300 * px)];
    BackView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:BackView];
    
    NSArray * titleArray = @[@"二维码绑定",@"公司代码绑定"];
    CGFloat BuidingButtonW = (BackView.width - 60 * px) / 2 ;
    CGFloat BuidingButtonY = 50 * px;
    CGFloat BuidingButtonH = 120 * px;
    for (int i = 0; i < titleArray.count; i ++) {
        /** 按钮*/
        UIButton * BuidingButton = [[UIButton alloc] init];
        BuidingButton.tag = i + 100;
        BuidingButton.x = (BuidingButtonW + 60 * px) * i ;
        BuidingButton.y = BuidingButtonY;
        BuidingButton.width = BuidingButtonW;
        BuidingButton.height = BuidingButtonH;
        BuidingButton.layer.cornerRadius = 60 * px;
        BuidingButton.layer.masksToBounds = YES;
        [BuidingButton setBackgroundImage:[UIImage imageNamed:@"底部按钮背景"] forState:UIControlStateNormal];
        [BuidingButton setTitle:titleArray[i] forState:UIControlStateNormal];
        BuidingButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
        [BuidingButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [BuidingButton addTarget:self action:@selector(BuidingButtonClcik:) forControlEvents:UIControlEventTouchUpInside];
        [BackView  addSubview:BuidingButton];
    }
    
}
- (void)BuidingButtonClcik:(UIButton *)sender{
    WYCLogFunc;
    if (sender.tag == 100) {
        MSScanBuidingController * ScanBuidingVC = [[MSScanBuidingController alloc] init];
        [self.navigationController pushViewController:ScanBuidingVC animated:NO];
    }else{
        MSCodeBuidingController * codeBuidingVC = [[MSCodeBuidingController alloc] init];
        [self.navigationController pushViewController:codeBuidingVC animated:NO];
    }
    
}
#pragma mark - UITableViewDataSource, UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.buidingArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 140 * px;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MSBuidingCorporationCell * cell = [MSBuidingCorporationCell cellWithTableView:tableView];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.BuidingModel = self.buidingArray[indexPath.row];
    cell.IDLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row + 1];
    cell.DeletegateButtonDelegate = self;
    [cell.DeleteButton addTarget:self action:@selector(DelegateClick) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
- (void)DelegateButtonClickSte_id:(NSString *)Ste_id{
    self.ste_id = Ste_id;
    
}

- (void)DelegateClick{
    NSDictionary * params = @{};
    
}
- (void)createNavigationBar{
    
    self.view.backgroundColor = backgroundcolor;
    naviagtionBar = [[CustomNavigationBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, [self navBarBottom])];
    [self.view addSubview:naviagtionBar];
    naviagtionBar.titleLabel.text = @"绑定公司";
    naviagtionBar.rightBtn.hidden = YES;
    naviagtionBar.rightBtnTitle.hidden = YES;
    [self.view addSubview:naviagtionBar];
    
    [naviagtionBar.leftBtn setImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
    [naviagtionBar.leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:NO];
}
@end
